<template>
  <div>
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="pyjm" label="" class="">
          <a-input v-model:value="formState.pyjm" class="w-64" placeholder="物品名称/物品编号/助记码/流水单号" />
        </a-form-item>
        <a-form-item name="dateRange" label="操作时间" class="">
          <a-range-picker v-model:value="formState.dateRange" class="w-64" format="YYYY-MM-DD " value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item label="操作人" class="w-[300px]">
          <a-input v-model:value="formState.czr" placeholder="请输入" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2">
      <div class="flex justify-end">
        <BaseExport type="物资" name="物品消耗统计" code="000322" :search-params="formState" class="mb-2">导出</BaseExport>
      </div>
      <BaseTable
        :data-source="tableData"
        fill
        :loading="loading"
        :scroll="{ x: 1800 }"
        column-code="000322"
        :pagination="pagination"
        @change="onPageChange"
        @expand="onShowChild"
      >
        <template #bodyCell="{ column, index }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
        </template>
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import { useRoute } from 'vue-router'
import useUserStore from '@/store/user'

const route = useRoute()
const { empId } = useUserStore()
const formRef = ref(null)
const formState = ref({
  dateRange: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  status: '',
  pxlx: '',
  pxzd: ''
})

const { loading, tableData, getData, pagination } = useTable({
  url: '/materials/consume/getDrugConsumptionList',
  formatParams: () => {
    return {
      stdate: formState.value.dateRange?.[0],
      eddate: formState.value.dateRange?.[1],
      ...formState.value,
      materialType: route.query?.routerType || undefined // 防疫物资
    }
  }
})

const getDepartInfo = async () => {
  const res = await post('/common/masterData/getByYsbhDepartment', { empId: empId })
  if (res.code !== 0) message.error(res.msg || '获取登录人科室失败')
  formState.value.ckbh = 'DEPTCODE' + res.data?.ksdm
  getData()
}

onMounted(() => {
  getDepartInfo()
})
</script>
